package com.translate.web.service.impl;

import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.ContractPicMapper;
import com.translate.entity.ContractPic;
import com.translate.entity.CustomerPoolMember;
import com.translate.web.param.contract.IContractPicAddParam;
import com.param.contract.IIdParam;
import com.translate.web.service.IContractPicService;
import com.basic.service.impl.BaseServiceImpl;
import com.translate.vo.contract.IContractPicVO;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
public class ContractPicServiceImpl extends BaseServiceImpl<ContractPic> implements IContractPicService {
	
	private RedisTemplate<String, List<ContractPic>> redisTemplate;

	@Autowired
	public void setRedisTemplate(RedisTemplate<String, List<ContractPic>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	public ContractPicServiceImpl(ContractPicMapper mapper) {
		super(mapper);
	}

	protected Class<ContractPic> getEntityClass() {
		return ContractPic.class;
	}

	
	
	
	@Override
	public <VO extends IContractPicAddParam> int batchInsert(List<VO> vos, int orderId) {
		
		if (null == vos) {
			return 0;
		}
		
		List<ContractPic> contractPics = new ArrayList<>();
		
		ContractPic contractPic;
		
		
		for (VO vo : vos) {
			
			contractPic = new ContractPic();
			
			contractPic.setContractId(orderId);
			
			contractPic.setPic(vo.getPic());
			
			contractPic.setType(vo.getType());
			
			contractPics.add(contractPic);
		}
		
		return this.batchInsert(contractPics);
		
	}

	@Override
	public <VO extends IContractPicVO> List<VO> getPics(int orderId, Class<VO> target) {
		List<VO> vos = new ArrayList<>();
		
		VO vo;
		
		List<ContractPic> orderPics = this.getOrderPicsToCache(orderId);
		
		try {
			
			for (ContractPic orderPic : orderPics) {
				
				vo = target.newInstance();
				
				vo.setContractId(orderPic.getContractId());
				
				vo.setId(orderPic.getId());
				
				vo.setPic(orderPic.getPic());
				
				vo.setType(orderPic.getType());
				
				vos.add(vo);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		return vos;
	}
	
	
	/**
	 * 获取订单图片备注
	 * @param orderId
	 * @return
	 */
	protected List<ContractPic> getOrderPicsToCache(int orderId) {
		
		String key = RedisConstant.getContractPicKey(orderId);
		
		ValueOperations<String, List<ContractPic>> valueOperations = this.redisTemplate.opsForValue();
		
		List<ContractPic> orderPics = valueOperations.get(key);
		
		if (null != orderPics) {
			return orderPics;
		}
		
		orderPics = this.getDataByIds(orderId);
		
		if (null == orderPics) {
			orderPics = new ArrayList<>(0);
			
			valueOperations.set(key, orderPics, 3, TimeUnit.SECONDS);
			
			return orderPics;
		}
		
		valueOperations.set(key, orderPics, 15, TimeUnit.SECONDS);

		return orderPics;
	}

	@Override
	public <S extends IContractPicAddParam> int updateStaff(List<S> list, int createUserId, int seaId) {
		
		
		return 0;
	}
	

	protected ContractPic getEntityInstance() {
		return new ContractPic();
	}

	protected int getId(CustomerPoolMember data) {
		return data.getId();
	}
	
	protected <S> boolean compareMasterCondition(S condition, ContractPic data) {

		IContractPicAddParam customerPoolAddMemberParam = (IContractPicAddParam) condition;

		Byte managerStatus = customerPoolAddMemberParam.getType();

		String pic = customerPoolAddMemberParam.getPic();

		return 0 == data.getType().compareTo(managerStatus) && customerPoolAddMemberParam.getPic().equals(pic);
	}

	protected <S> void toEntity(ContractPic customerPoolMember, S obtainedFromTheMap, int assocId, int userId) {

		IContractPicAddParam customerPoolAddMemberParam = (IContractPicAddParam) obtainedFromTheMap;

		customerPoolMember.setContractId(assocId);

		Integer id = null;
		
		if (obtainedFromTheMap instanceof IIdParam) {
			id = ((IIdParam) obtainedFromTheMap).getId();
			
			if (null != id) {
				customerPoolMember.setId(id);
			}
		}

		customerPoolMember.setPic(customerPoolAddMemberParam.getPic());

		customerPoolMember.setType(customerPoolAddMemberParam.getType());

	}
}